//
//  HJBankListVC.m
//  project
//
//  Created by jc on 2019/4/15.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJBankListVC.h"
#import "HJBankListCell.h"
#import "HJAddBankeVC.h"
#import "HJBankEmpty.h"

@interface HJBankListVC ()
@property (nonatomic,strong) NSMutableArray <HJBankModel *>* dataArray;

@end

@implementation HJBankListVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadNetworkData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"我的银行卡";
    self.rightNavImageName = @"bankAdd";
}

#pragma mark --  导航右边按钮点击事件
-(void)didClickRightNavAction{
    [self rightButtonClicked];
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    // 空太视图
    HJBankEmpty * emptyView = [HJBankEmpty initView];
    @weakify(self)
    emptyView.addDidClickBlock = ^{
        @strongify(self)
        [self rightButtonClicked];
    };
    self.tableView.emptyView = emptyView;
}

-(NSMutableArray <HJBankModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(void)loadNetworkData {
    WS(weakSelf)
    NSString * url = [Api stringByAppendingString:BankCardApi];
    [HJHttpTool get:url params:@{@"userId":[HJUserManager getUserModel].userId} success:^(HJResponeds *result) {
        LKLog(@"%@",result.data);
        [weakSelf.dataArray removeAllObjects];
        NSArray * arr = [NSArray yy_modelArrayWithClass:[HJBankModel class] json:result.data];
        [weakSelf.dataArray addObjectsFromArray:arr];
        [weakSelf.tableView reloadData];
    } failure:^(id result) {
        [weakSelf showErrorHUDWithError:result];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJBankListCell * listCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!listCell) {
        listCell = [[NSBundle mainBundle] loadNibNamed:@"HJBankListCell" owner:nil options:nil].firstObject;
    }
    [listCell loadBankDataModel:self.dataArray[indexPath.row]];
    return listCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 135;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    UITableViewRowAction * deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:nil handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSString * deleteUrl = [Api stringByAppendingString:DeleteBankCardApi];
        HJBankModel * model = weakSelf.dataArray[indexPath.row];
        [HJHttpTool post:deleteUrl params:@{@"bankId":model.bankId,@"userId":[HJUserManager getUserModel].userId} success:^(HJResponeds *result) {
            [weakSelf showSuccessHUDWithStr:@"解除绑定成功"];
            [weakSelf.dataArray removeObject:model];
            dispatch_async(dispatch_get_main_queue(), ^{
                [tableView reloadData];
            });
        } failure:^(id result) {
            [weakSelf showErrorHUDWithError:result];
        }];
    }];
    deleteAction.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"celldelete"]];
    return @[deleteAction];
}

//-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath  API_AVAILABLE(ios(11.0)) {
//    WS(weakSelf)
//    UIContextualAction * deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//        NSString * deleteUrl = [Api stringByAppendingString:DeleteBankCardApi];
//        HJBankModel * model = weakSelf.dataArray[indexPath.row];
//        [HJHttpTool post:deleteUrl params:@{@"bankId":model.bankId,@"userId":[HJUserManager getUserModel].userId} success:^(HJResponeds *result) {
//            [weakSelf showSuccessHUDWithStr:@"解除绑定成功"];
//            [weakSelf.dataArray removeObject:model];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [tableView reloadData];
//            });
//        } failure:^(id result) {
//            [weakSelf showErrorHUDWithError:result];
//        }];
//        completionHandler(YES);
//    }];
//    deleteAction.image = [setImage(@"celldelete") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    deleteAction.backgroundColor = HJColorRGB(243);
//    
//    return [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
//}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (self.dataArray.count==0) return UIView.new;
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    view.backgroundColor = [UIColor clearColor];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(34, 46, view.width-68, 40);
    [rightBtn setTitleColor:[UIColor colorWithHex:0xffffff] forState:0];
    [rightBtn setBackgroundColor:[UIColor colorWithHex:0x2a2a2a]];
    [rightBtn setImage:setImage(@"icon_gift_add_bank_card") forState:0];
    [rightBtn rounded:UIRectCornerAllCorners radius:5];
    [rightBtn setTitle:@"添加银行卡" forState:0];
    [rightBtn addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:rightBtn];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.dataArray.count==0) return 0.001;
    return 96;
}

#pragma mark --添加银行卡
-(void)rightButtonClicked {
    HJAddBankeVC * bankVC = [HJAddBankeVC new];
    @weakify(self)
    bankVC.updateListBlock = ^{
        @strongify(self)
        [self loadNetworkData];
    };
    [self.navigationController pushViewController:bankVC animated:YES];
}

@end


@implementation HJBankModel


@end
